package diShiSanZhou;

import java.text.DecimalFormat;
import java.util.Random;

public class Factory {
    //存储银行存款
    double saving =0;
    //随机数
    Random r=new Random();
    //规范浮点数
    DecimalFormat decimalFormat=new DecimalFormat("#.0");
    int count = 0;

    //存款
    public synchronized void  banDeposit() throws Exception{
        double money=Double.valueOf(decimalFormat.format(r.nextDouble()*100+1));
        //double money=Double.valueOf(decimalFormat.format(r.nextDouble(100)+1));
        if(count == 5){
            this.wait();
            count = 0;
        }else {
            saving += money;
            System.out.println("存入金额是：" + money + ",当前余额是：" + String.format("%.1f", saving));
            Thread.sleep(200);
            count++;
            this.notify();
        }

    }

    //取款
    public synchronized void bandWithdrawal() throws Exception{
        double money=Double.valueOf(decimalFormat.format(r.nextDouble()*100+1));
        if(money >= saving){
            System.out.println("取款金额是：" + money + ",当前余额是：" + String.format("%.1f", saving) + ",余额不足");
            this.wait();
        }else {
            Thread.sleep(200);
            saving -= money;
            System.out.println("取款金额是：" + money + ",当前余额是：" + String.format("%.1f", saving));
            this.notify();
        }
    }
}
